% Script to download USGS Dst minute values
% and process it

startDate = datenum(2011,10,01);
endDate = datenum(2011,10,31);

baseurl = 'http://geomag.usgs.gov/data/indices/Dst_minute/';

startIndex = 1;
endIndex = 1440;

ndays = length(startDate:endDate);

DstMinData = zeros([1,ndays*1440]);
FdayMinData = nan([1,ndays*1440]);

for i = 1:ndays,

    thisday = ['Dst4_' datestr(startDate + i - 1, 'mmddyyyy') '.min'];
    
    [s, flag] = urlread([baseurl thisday]);
    
    if flag == 1,
        
        formattedData = textscan(s,'%s%s%f','Headerlines',14);
        
        if length (formattedData{1}) == 1440,
            
            
            dayCell = datenum(formattedData{1});
            
            hourCell = datenum(formattedData{2});
            
            
            DstMinData(startIndex:endIndex) = cell2mat(formattedData(3));
            
            FdayMinData(startIndex:endIndex) = dayCell + hourCell - floor(hourCell);
            
            fprintf('Done %s \n', thisday);
            
        else
            
            fprintf('Number of data on %s is %d\n', thisday , length (formattedData{1}));
            
        end;
        
    else,
        
        fprintf('Data for %s is not available\n', thisday);
        
    end;
    
    startIndex = endIndex + 1;
    endIndex = endIndex + 1440;

    
end;
    
    
